package com.kevin.io.jdk.bio;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @author Kevin Liu
 * @date 2020/8/26 9:02 下午
 */
public class Client {

    public static void main(String[] args) throws IOException {
        Socket s = new Socket("localhost", 8080);
        OutputStream out = s.getOutputStream();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入：");
            String msg = scanner.nextLine();
            if(msg.equals("close")) {
                s.shutdownOutput();
                break;
            }
            msg += "\r\n";
            out.write(msg.getBytes(StandardCharsets.UTF_8));
        }
        InputStream inputStream = s.getInputStream();
        BufferedReader bufferedReader = IoUtils.getBufferedReader(inputStream);
        String msg;
        while ((msg = bufferedReader.readLine()) != null) {
            if (msg.length() == 0) {
                break;
            }
            System.out.println(msg);
        }
        scanner.close();
        s.close();
    }
}
